class Observer {
    constructor(data) {
        this.walk(data)
    }

    walk(data) {
        // 判断data是否是对象
        if (!data || typeof data !== "object") return
        // 遍历所有属性
        Object.keys(data).forEach(key => {
            this.defineReactive(data, key, data[key])
        })
    }

    defineReactive(obj, key, val) {
        const self = this
        let dep = new Dep()
        self.walk(val)
        Object.defineProperty(obj, key, {
            enumerable: true,
            configurable: true,
            get() {
                Dep.target && dep.addSub(Dep.target)
                return val
            },
            set(v) {
                if (v === val) return
                val = v
                self.walk(v)
                dep.notify()
            }
        })
    }
}